package com.zoweunion.mechanic.controller;

import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.entity.TSSsMessageRecordEntity;
import com.zoweunion.mechanic.entity.dto.TSSsMessageRecordDTO;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.ITSSsMessageRecordService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ${comments}
 *
 * @author wxx
 * @date 2023-05-22
 */
@RestController
@RequestMapping(path = "/tSSsMessageRecord", produces = "application/json;charset=UTF-8")
//@Api(value = "/tSSsMessageRecord", tags = {"测试文档","测试文档1"}, produces = "application/json;charset=UTF-8")

public class TSSsMessageRecordController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(TSSsMessageRecordController.class);

    @Autowired
    private ITSSsMessageRecordService tSSsMessageRecordService;

    @GetMapping("/listPage")
//     @ApiOperation(value = "条件查询列表分页", notes = "条件查询列表分页")
    public ResponseBean listPage(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {

        params.put("user_id", getCurrentUser(request).getId());
        PageInfo info = tSSsMessageRecordService.listPage(params);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }


    /**
     * 新增
     */
    @PostMapping
//    @ApiOperation(value = "新增", notes = "")
    public ResponseBean create(@RequestBody @Validated TSSsMessageRecordEntity tSSsMessageRecord, HttpServletRequest request) {

        try {
            tSSsMessageRecordService.save(tSSsMessageRecord);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } catch (Exception e) {
            return new ResponseBean(Constants.CODE_ERROR, e.getMessage(), "");
        }


    }


    /**
     * 更新
     */
    @PostMapping("/update")
//    @ApiOperation(value = "更新", notes = "")
    public ResponseBean update(@RequestBody TSSsMessageRecordEntity tSSsMessageRecord, HttpServletRequest request) {

        if (tSSsMessageRecord.getId() != null) {
            tSSsMessageRecordService.updateById(tSSsMessageRecord);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } else {
            return new ResponseBean(Constants.CODE_ERROR, "id必填", "");
        }
    }


    /**
     * 更新（根据ids批量更新数据,更新字段都是相同的值)
     */
    @PutMapping("/batchUpdate")
//    @ApiOperation(value = "批量更新", notes = "")
    public ResponseBean batchUpdate(@RequestBody TSSsMessageRecordDTO tSSsMessageRecord, HttpServletRequest request) {

        if (tSSsMessageRecord.getIds() != null) {

            List<TSSsMessageRecordEntity> list = new ArrayList<>();
            for(String id:tSSsMessageRecord.getIds()){
                TSSsMessageRecordEntity item = new TSSsMessageRecordEntity();
                // 复制共同属性
                BeanUtils.copyProperties(tSSsMessageRecord,item);
                // 根据id刷新
                item.setId(id);
                list.add(item);
            }

            tSSsMessageRecordService.updateBatchById(list);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } else {
            return new ResponseBean(Constants.CODE_ERROR, "ids必填", "");
        }
    }


    /**
     * 详情
     */
    @GetMapping("/{id}")
//    @ApiOperation(value = "详情", notes = "")
    public ResponseBean detail(@PathVariable("id") String id) {
        try {

            TSSsMessageRecordEntity tSSsMessageRecord = tSSsMessageRecordService.getById(id);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", tSSsMessageRecord);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseBean(Constants.CODE_ERROR, "操作失败", "");
        }

    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
//    @ApiOperation(value = "删除", notes = "")
    public ResponseBean delete(@RequestBody Long[] ids) {
        try {

            tSSsMessageRecordService.removeByIds(Arrays.asList(ids));
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseBean(Constants.CODE_ERROR, "操作成功", "");
        }
    }

//    /**
//     * 列表导出
//     */
//    @GetMapping("/listExport")
//    public void listExport(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
//        List<TSSsMessageRecordExcel> info = tSSsMessageRecordService.listExport(params);
//        ExcelUtils.exportExcelToTarget(response, "列表导出", info, TSSsMessageRecordExcel.class);
//    }


}
